﻿using System;
using System.Collections.Generic;
using System.Linq;
using FBEntities.FBConnections.User;
using Newtonsoft.Json.Linq;
using XDAFacebook;
using XFControls.Utils;

namespace FBEntities.FBObjects.User
{
    public class FBInterest : IFBObject
    {
        public string category;
        public string id;
        public string name;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

    public class FBCheckinTags : IFBObject
    {
        public List<FBProxyPerson> data = new List<FBProxyPerson>();

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }


    public class FBCheckin : IFBObject
    {
        public string id;
        public string message;
        public FBFrom from;
        public FBCheckinTags tags;
        public FBPlace place;
        public FBApplication application;
        public DateTime created_time;
        public FBComments comments;
        public FBObjectLikes likes;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        public WebDownloaderWrapper Create()
        {
            if (place == null)
                return null;


            string lat = place.location.latitude.ToString();
            string lon = place.location.longitude.ToString();

            lat = lat.Replace(',', '.');
            lon = lon.Replace(',', '.');


            string authKeyCode = FBParseUtilities.authKeySplit;
            string coordinates = "{\"latitude\":\"" + lat + "\", \"longitude\": \"" + lon + "\"}";

            var args = new Dictionary<string, string> { { "access_token", authKeyCode }, { "place", place.id }, { "coordinates", coordinates } };
            if (!string.IsNullOrEmpty(message))
                args.Add("message", message);

            if (tags != null && tags.data.Any())
            {
                string t = "";
                foreach (var tag in tags.data)
                {
                    if (string.IsNullOrEmpty(t))
                        t = tag.id;
                    else
                        t += "," + tag.id;
                }
                args.Add("tags", t);
            }

            return new WebDownloaderWrapper("https://graph." + Settings.Domain + ".com/me/checkins") { PostData = args };
        }

        public FBPost ToPost()
        {
            string cap = "";

            if (tags != null)
                foreach (var tag in tags.data)
                {
                    if (string.IsNullOrEmpty(cap))
                        cap = "Tags: " + tag.name;
                    else
                        cap += ", " + tag.name;
                }

            FBPost ret = new FBPost
                         {
                             id = id,
                             from = from,
                             name = place.name,
                             message = message,
                             created_time = created_time,
                             comments = comments,
                             caption = cap
                         };

            return ret;
        }

        #endregion
    }

    public class FBObjectLikes : IFBObject
    {
        public List<FBInterest> data;

        public FBObjectLikes()
        {
            data = new List<FBInterest>();
        }

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

}